﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TruyenCuoi.Entity;
using VideoCookBook.Entity;
using System.Net.NetworkInformation;
using System.Windows;

namespace TruyenCuoi.ViewModel
{
    public class MainViewModel : BindableBase
    {
        private StoryResponse data = new StoryResponse();
        private List<ReturnData> listStory = new List<ReturnData>();

        public List<ReturnData> ListStory
        {
            get { return listStory; }
            set { SetProperty(ref listStory, value); }
        }
        public MainViewModel()
        {
            CallServiceStory();
        }

        private void CallServiceStory()
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                WebClient wb = new WebClient();
                wb.DownloadStringCompleted += wb_DownloadStringCompleted;
                wb.DownloadStringAsync(new Uri("http://www.wservice.somee.com/theloai/getlisttheloai"));
            }
            else
                MessageBox.Show("Please check NetWork connection", "Error NetWork Connection", MessageBoxButton.OK);
        }

        void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    data = JsonConvert.DeserializeObject<StoryResponse>(e.Result);
                }
                else
                {
                    MessageBox.Show("Sorry for load data error", "Error data", MessageBoxButton.OK);
                }
            }
            catch (Exception ex)
            {

            }
            ListStory = data.returnData;
        }
    }
}
